package net.tv90.bfmccore.listener.effect;

import net.tv90.bfmccore.helper.FactionHelper;
import org.bitterorange.flansmodslain.FlansModAPI;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.*;

import static net.tv90.bfmccore.helper.FactionHelper.*;
import static net.tv90.bfmccore.helper.FlanHelper.getWeaponType;
import static net.tv90.bfmccore.helper.MCHHelper.getMCHEntitiesSoundFilterType;
import static net.tv90.bfmccore.helper.MCHHelper.getMCHEntitiesType_Lang;

public class SpotEffect extends RadioEffect {
    private static final Map<String, String> typeMap = new HashMap<>();
    private static final String[] vehicleTypePriority = {"JET", "HELI", "AA", "TANK", "LAV", "BOAT", "VEHICLE", "AT", "SNIPER", "SOLDIER"};

    static {
        typeMap.put("AA", "防空车!");
        typeMap.put("AT", "反坦克兵!");
        typeMap.put("BOAT", "船只!");
        typeMap.put("HELI", "直升机!");
        typeMap.put("JET", "飞机!");
        typeMap.put("LAV", "装甲车!");
        typeMap.put("SNIPER", "侦察兵!");
        typeMap.put("SOLDIER", "步兵!");
        typeMap.put("TANK", "坦克!");
        typeMap.put("VEHICLE", "载具!");
    }

    public SpotEffect(String soundPrefix, String name) {
        super(soundPrefix, name);
    }

    public static String getEnemyType(Entity p) {
        if(p instanceof Player) {
            String vehicle = "AIR";
            if (p.getVehicle() != null) {
                vehicle = FlansModAPI.getPlayerVehicle((Player) p);
            }
            if (!"AIR".equals(vehicle)) {
                String type = getWeaponType(vehicle);
                switch (type) {
                    case "JET":
                    case "Bomber":
                        return "JET";
                    case "AA":
                        return "AA";
                    case "MBT":
                        return "TANK";
                    case "IFV":
                        return "LAV";
                    case "FAC":
                        return "BOAT";
                    case "Heli":
                    case "ScoutHeli":
                        return "HELI";
                    default:
                        return "VEHICLE";
                }
            }
            if ("ENG".equals(FactionHelper.getKit2((Player) p))) {
                return "AT";
            }
            if ("REC".equals(FactionHelper.getKit2((Player) p))) {
                return "SNIPER";
            }
        }
        return "SOLDIER";
    }

    public static String getEnemyTypeName(Entity p) {
        return typeMap.get(getEnemyType(p));
    }

    @Override
    public void playEffectForPlayer(Player p, String country) {
        List<Entity> targetEnemyEntities = getTargetEnemyEntities(p, 350, 15);
        String soundFilterType = getMCHEntitiesSoundFilterType(p);
        List<String> vehicleType = new ArrayList<>();
        if (!targetEnemyEntities.isEmpty()) {
            FlansModAPI.markEntity(p, 5, targetEnemyEntities);

            for (Entity entity : targetEnemyEntities) {
                vehicleType.add(getEnemyType(entity));
            }
            vehicleType.sort(new VehicleTypeComparator());

            if(!vehicleType.isEmpty()) {
                for (Player teamer : FactionHelper.getTeammates(p)) {
                    if (FactionHelper.isInSameWorld(p, teamer)) {
                        //[队伍]TV90(步兵, 100m) >>> 发现敌军步兵!
                        teamer.sendMessage("§a[队伍] " + p.getName() + "(" + getMCHEntitiesType_Lang(p) + ", " +
                                (int) teamer.getLocation().distance(p.getLocation()) + "m) >>> " + name + typeMap.get(vehicleType.get(0)));
                    }
                    teamer.playSound(teamer.getLocation(), "filter." + soundFilterType + "_FILTER", 100.0F, 1.0F);
                    teamer.playSound(teamer.getLocation(), "faction." + country + "_" + sound + "_" + vehicleType.get(0), 100.0F, 1.0F);
                }
                p.sendMessage("§a[队伍] " + p.getName() + " >>> " + name + typeMap.get(vehicleType.get(0)));
                p.playSound(p.getLocation(), "faction." + country + "_" + sound + "_" + vehicleType.get(0), 100.0F, 1.0F);
            }

        } else {
            for (Player teamer : FactionHelper.getTeammates(p)) {
                if(FactionHelper.isInSameWorld(p, teamer)) {
                    teamer.sendMessage("§a[队伍] " + p.getName() + "(" + getMCHEntitiesType_Lang(p) + ", " +
                            (int) teamer.getLocation().distance(p.getLocation()) + "m) >>> " + name + "!");
                    teamer.playSound(teamer.getLocation(), "filter." + soundFilterType + "_FILTER", 100.0F, 1.0F);
                    teamer.playSound(teamer.getLocation(), "faction." + country + "_SPOT_SOLDIER", 100.0F, 1.0F);
                }
            }
            p.sendMessage("§a[队伍] " + p.getName() + " >>> " + name + "!");
            p.playSound(p.getLocation(), "faction." + country + "_SPOT_SOLDIER", 100.0F, 1.0F);
        }
    }

    @Override
    public void playEffectForTeamExcludeSelf(Player p, Player teamer, String country, String soundFilterType) {

    }

    @Override
    public void playEffectForTeam(Player p, Player teamer) {

    }


    static class VehicleTypeComparator implements Comparator<String> {
        @Override
        public int compare(String type1, String type2) {
            int index1 = Arrays.asList(vehicleTypePriority).indexOf(type1);
            int index2 = Arrays.asList(vehicleTypePriority).indexOf(type2);

            if (index1 == -1) {
                return 1;
            } else if (index2 == -1) {
                return -1;
            }

            return Integer.compare(index1, index2);
        }
    }
}
